package com.kakarote.job.crm;
import com.kakarote.core.common.Const;
import com.kakarote.core.common.Result;
import com.kakarote.core.entity.UserInfo;
import com.kakarote.core.feign.admin.service.AdminService;
import com.kakarote.core.feign.crm.service.CrmService;
import com.kakarote.core.redis.Redis;
import com.kakarote.core.utils.UserUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 客户定时放入公海
 */
@Component
@Slf4j
public class CrmCustomerJob {

    @Autowired
    private CrmService crmService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private Redis redis;

    @XxlJob("CrmCustomerJob")
    public ReturnT<String> demoJobHandler(String param) {
        try {
            UserInfo userInfo = UserUtil.setUser(UserUtil.getSuperUser());
            redis.setex("CrmCustomerJob", Const.MAX_USER_EXIST_TIME, userInfo);
            Result result = crmService.putInInternational();
            if (!result.hasSuccess()) {
                ReturnT<String> fail = ReturnT.FAIL;
                fail.setMsg(result.getMsg());
                return fail;
            }
        } catch (Exception e) {
            XxlJobLogger.log(e);
        } finally {
            redis.del("CrmCustomerJob");
            UserUtil.removeUser();
        }

        XxlJobLogger.log("客户定时放入公海执行完成");
        return ReturnT.SUCCESS;
    }
}
